#include "Rational.h"
Rational::Rational()
{
    this->numarator=0.0;
    this->numitor=0.0;
}
Rational::Rational(int x, int y)
{
    this->numarator=x;
    this->numitor=y;
}
ostream& operator<<(ostream& afiseaza, const Rational& x)
{
    afiseaza<<x.numarator<<"/"<<x.numitor;
    return afiseaza;
}
int Rational::getNumarator()
{
    return this->numarator;
}
int Rational::getNumitor()
{
    return this->numitor;
}
void Rational::setNumarator(int x)
{
    this->numarator=x;
}
void Rational::setNumitor(int x)
{
    this->numitor=x;
}
int Rational::ncomun(Rational x)
{
	int a,b,k,i;
	if (this->getNumitor()==x.getNumitor()) return this->getNumitor();
	else
	{
		a=this->getNumitor();
		b=x.getNumitor();
		if(a<b)  k=b;
		else k=a;
		for(i=k;;i++)
		if(i%a==0 && i%b==0) return i;
	}
}
Rational Rational::simplificare()
{
	int a,b,c,d;
	a=this->getNumarator();
	b=this->getNumitor();
	c=a;
	d=b;
	while(c!=d){
	if (c>d) c=c-d;
	else d=d-c; }
	a=a/c;
	b=b/c;
	this->setNumarator(a);
	this->setNumitor(b);
}
Rational Rational::operator+(Rational x)
{
  int a,b,c,d,n;
  a=this->getNumarator();
  b=this->getNumitor();
  c=x.getNumarator();
  n=this->ncomun(x);
  d=x.getNumitor();
  a=a*n/b;
  b=n;
  c=c*n/d;
  Rational s=Rational(a+c,b);
  s.simplificare();
  return s;
}
Rational Rational::operator-(Rational x)
{
  int a,b,c,d,n;
  a=this->getNumarator();
  b=this->getNumitor();
  c=x.getNumarator();
  d=x.getNumitor();
  n=this->ncomun(x);
  a=a*n/b;
  b=n;
  c=c*n/d;
  Rational dif=Rational(a-c,b);
  dif.simplificare();
  return dif;
}
Rational Rational::operator*(Rational x)
{
    int a,b,c,d;
    a=this->getNumarator();
    b=this->getNumitor();
    c=x.getNumarator();
    d=x.getNumitor();
    Rational prod=Rational(a*c,b*d);
    prod.simplificare();
    return prod;
}
Rational Rational::operator/(Rational x)
{
    int a,b,c,d;
    a=this->getNumarator();
    b=this->getNumitor();
    c=x.getNumarator();
    d=x.getNumitor();
    Rational imp=Rational(a*d,b*c);
    imp.simplificare();
    return imp;
}
bool Rational::operator==(Rational x)
{
    int a,b,c,d,n;
    a=this->getNumarator();
    b=this->getNumitor();
    c=x.getNumarator();
    d=x.getNumitor();
    n=this->ncomun(x);
    a=a*n/b;
    c=c*n/d;
    if (a==c) return true;
    else return false;
}
bool Rational::operator<(Rational x)
{
    int a,b,c,d,n;
    a=this->getNumarator();
    b=this->getNumitor();
    c=x.getNumarator();
    d=x.getNumitor();
    n=this->ncomun(x);
    a=a*n/b;
    c=c*n/d;
    if (a<c) return true;
    else return false;
}
bool Rational::operator>(Rational x)
{
    int a,b,c,d,n;
    a=this->getNumarator();
    b=this->getNumitor();
    c=x.getNumarator();
    d=x.getNumitor();
    n=this->ncomun(x);
    a=a*n/b;
    c=c*n/d;
    if (a>c) return true;
    else return false;
}
bool Rational::operator<=(Rational x)
{
    int a,b,c,d,n;
    a=this->getNumarator();
    b=this->getNumitor();
    c=x.getNumarator();
    d=x.getNumitor();
    n=this->ncomun(x);
    a=a*n/b;
    c=c*n/d;
    if (a<=c) return true;
    else return false;
}
bool Rational::operator>=(Rational x)
{
    int a,b,c,d,n;
    a=this->getNumarator();
    b=this->getNumitor();
    c=x.getNumarator();
    d=x.getNumitor();
    n=this->ncomun(x);
    a=a*n/b;
    c=c*n/d;
    if (a>=c) return true;
    else return false;
}
sirRational::sirRational(Rational s[100])
{
    Rational nul=Rational();
    int i;
    for(i=0 ;s[i].getNumitor()!=0;i++)
    {
        this->sir[i]=s[i];
    }
    this->sir[i]=nul;
}
Rational sirRational::getElement(int i)
{
    return this->sir[i];
}
int sirRational::nrElemente()
{
    int i;

    for(i=0;this->getElement(i).getNumitor()!=0;i++);
    return i;
}
void sirRational::afiseaza()
{
    int n=this->nrElemente();
    for(int i=0;i<n;i++) cout<<this->getElement(i)<<endl;
}
Rational sirRational::suma()
{
    int i,n=this->nrElemente();
    Rational s;
    s=this->getElement(0);
    cout<<s;
    for(i=1;i<n;i++)
    {
        s=s+this->getElement(i);
    }
    return s;
}
void sirRational::ireductibil()
{
    int n=this->nrElemente();
    for(int i=0;i<n;i++) this->sir[i].simplificare();
}
void sirRational::ordonare()
{
    int i,j,n=this->nrElemente();
    Rational aux;
    Rational unitate=Rational(1.0,1.0);
    for(i=0;i<n-1;i++)
    for(j=i+1;j<n;j++)
    if(this->getElement(i)>this->getElement(j))
    {
        aux=this->sir[i];
        this->sir[i]=this->sir[j];
        this->sir[j]=aux;
    }
    if(this->getElement(0)<unitate) cout<<endl<<"Cel mai mic element subunitar este:"<<this->getElement(0)<<endl;
    if(this->getElement(n-1)>unitate) cout<<"Cel mai mare element supraunitar este:"<<this->getElement(n-1)<<endl;
}
